﻿Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Imports System.Xml
Public Class Parent1
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Public Sub filltree()
        Try
            Dim ds As New DataSet
            Dim str As String, i As Integer, j As Integer
            TreeView1.Nodes.Clear()
            TreeView1.CheckBoxes = True
            If txttype.Text = "2003" Then
                If txtpassword.Text = "" Then
                    str = "provider=microsoft.jet.oledb.4.0; data source=" & txtdb.Text & ";"
                Else
                    str = "provider=microsoft.jet.oledb.4.0; data source=" & txtdb.Text & ";jet oledb:database password='" & txtpassword.Text & "';"
                End If
            ElseIf txttype.Text = "2007" Then
                If txtpassword.Text = "" Then
                    str = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" & txtdb.Text & ";"
                Else
                    str = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" & txtdb.Text & ";jet oledb:database password='" & txtpassword.Text & "';"
                End If
            ElseIf txttype.Text = "XML" Then
                Dim xmlFile As XmlReader
                xmlFile = XmlReader.Create(txtdb.Text, New XmlReaderSettings())
                TreeView1.Nodes.Clear()
                ds.ReadXml(xmlFile)
                TreeView1.CheckBoxes = False
                Dim a As New View_Table
                a.MdiParent = Me
                a.fillxml(ds, System.IO.Path.GetFileNameWithoutExtension(txtdb.Text))
                TreeView1.Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(txtdb.Text))
                For i = 0 To ds.Tables(0).Columns.Count - 1
                    TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                Next
                Exit Sub
            End If
            con = New OleDbConnection(str)
            con.Open()
            Dim dt As System.Data.DataTable
            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim d As DataRow
            For Each d In dt.Rows
                If UCase(d("table_type").ToString) = "TABLE" Then
                    TreeView1.Nodes.Add(d("Table_name").ToString)
                    str = "select * from " & d("Table_name").ToString
                    Dim ad As OleDbDataAdapter
                    ad = New OleDbDataAdapter(str, con)
                    ds = New DataSet
                    ad.Fill(ds, "stock")
                    Dim k As Integer
                    For i = 0 To ds.Tables(0).Columns.Count - 1
                        TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                        TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                    Next
                    ds.Clear()
                    j = j + 1
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub ViewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Table
                a.MdiParent = Me
                a.StartPosition = FormStartPosition.CenterScreen
                a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub
    Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
        Dim NodeClicked As TreeNode
        NodeClicked = Me.TreeView1.GetNodeAt(e.X, e.Y)
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.TreeView1.SelectedNode = NodeClicked
            Try
                If TreeView1.SelectedNode.Level = 0 Then
                    Me.Contextlevel1.Show(TreeView1, New Point(e.X, e.Y))
                ElseIf TreeView1.SelectedNode.Level = 1 Then
                    Me.contextlevel2.Show(TreeView1, New Point(e.X, e.Y))
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub RemoveToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveToolStripMenuItem1.Click
        TreeView1.SelectedNode.Remove()
    End Sub

    Private Sub RemoveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveToolStripMenuItem.Click
        TreeView1.SelectedNode.Remove()
    End Sub

    Private Sub NewWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewWindowToolStripMenuItem.Click
        Dim a As New View_Table
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String
        If TreeView1.SelectedNode.Checked = True Then
            txtquery = txtquery & " SELECT "
            c = 0
            While j < TreeView1.SelectedNode.Nodes.Count
                If TreeView1.SelectedNode.Nodes(j).Checked = True Then
                    txtquery = txtquery & " " & TreeView1.SelectedNode.Nodes(j).Text & ","
                    c = c + 1
                End If
                j = j + 1
            End While
            If c = 0 Then
                txtquery = txtquery & " * "
            Else
                txtquery = txtquery.Substring(0, txtquery.Length - 1)
            End If
            txtquery = txtquery & " FROM " & TreeView1.SelectedNode.Text & vbCrLf
            a.StartPosition = FormStartPosition.CenterScreen
            a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.SelectedNode.Text)
            j = 0
            txtquery = ""
            'TreeView1.SelectedNode.Checked = False
        Else
            MsgBox("Selected Record not checked", MsgBoxStyle.Information)
        End If
        i = i + 1
    End Sub

    Private Sub ToolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem6.Click
       filltree()
    End Sub
    Private Sub Parent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim a As New Backcover
            a.MdiParent = Me
            a.Show()
            a.Dock = DockStyle.Fill
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub ReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Report
                a.MdiParent = Me
                a.StartPosition = FormStartPosition.CenterScreen
                a.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub OpenProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProjectToolStripMenuItem.Click
        Me.Enabled = False
        NewName.Show()
    End Sub

    Private Sub LoadSettingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadSettingToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub GenerateReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateReportToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Report
                a.MdiParent = Me
                a.StartPosition = FormStartPosition.CenterScreen
                a.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub ReportNewWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportNewWindowToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Report
                a.StartPosition = FormStartPosition.CenterScreen
                '  a.MdiParent = Me
                a.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub GenerateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateToolStripMenuItem.Click
        Dim a As New View_Table
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String
        If TreeView1.SelectedNode.Checked = True Then
            txtquery = txtquery & " SELECT "
            c = 0
            While j < TreeView1.SelectedNode.Nodes.Count
                If TreeView1.SelectedNode.Nodes(j).Checked = True Then
                    txtquery = txtquery & " " & TreeView1.SelectedNode.Nodes(j).Text & ","
                    c = c + 1
                End If
                j = j + 1
            End While
            If c = 0 Then
                txtquery = txtquery & " * "
            Else
                txtquery = txtquery.Substring(0, txtquery.Length - 1)
            End If
            a.MdiParent = Me
            a.StartPosition = FormStartPosition.CenterScreen
            txtquery = txtquery & " FROM " & TreeView1.SelectedNode.Text & vbCrLf
            a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.SelectedNode.Text)
            j = 0
            txtquery = ""
            'TreeView1.SelectedNode.Checked = False
        Else
            MsgBox("Selected Record not checked", MsgBoxStyle.Information)
        End If
        i = i + 1
    End Sub

    Private Sub ViewReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewReportToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Table
                a.MdiParent = Me
                a.StartPosition = FormStartPosition.CenterScreen
                a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.Nodes(i).Text)
                Dim b As New View_Report
                b.MdiParent = Me
                b.StartPosition = FormStartPosition.CenterScreen
                b.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub ReportSeprateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportSeprateToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Report
                a.StartPosition = FormStartPosition.CenterScreen
                a.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub ViewSeprateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewSeprateToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Table
                a.StartPosition = FormStartPosition.CenterScreen
                a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub ViewAndReportSeprateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewAndReportSeprateToolStripMenuItem.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String

        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                Dim a As New View_Table
                a.StartPosition = FormStartPosition.CenterScreen
                a.filldatagridview(txtquery, con.ConnectionString, 2003, TreeView1.Nodes(i).Text)
                Dim b As New View_Report
                b.StartPosition = FormStartPosition.CenterScreen
                b.fillreport(txtquery, con.ConnectionString, TreeView1.Nodes(i).Text)
                j = 0
                txtquery = ""
                TreeView1.Nodes(i).Checked = False
            End If
            i = i + 1
        End While
    End Sub

    Private Sub AboutUsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutUsToolStripMenuItem.Click
        About_Us.ShowDialog()
    End Sub

    Private Sub ExportToXmlToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToXmlToolStripMenuItem.Click
        xml_Export.filldatagridview(con.ConnectionString)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        End
    End Sub

    Private Sub OpenDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Enabled = False
        NewName.Show()
    End Sub

    Private Sub JjToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class
